﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace Read_Unread_Solution.Mark_Item_Unread
{
    class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
    {
        bool oldValue;

        public DisabledItemEventsScope()
        {
            this.oldValue = base.EventFiringEnabled;
            base.EventFiringEnabled = false;
        }

        #region IDisposable Members

        public void Dispose()
        {
            base.EventFiringEnabled = oldValue;
        }

        #endregion
    }
    /// <summary>
    /// List Item Events
    /// </summary>
    public class Mark_Item_Unread : SPItemEventReceiver
    {
        /// <summary>
        /// An item is being updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                {
                   
                    SPUserToken systoken = properties.ListItem.Web.Site.SystemAccount.UserToken;
                    using (SPSite site = new SPSite(properties.ListItem.Web.Site.Url, systoken))
                    {
                        using (SPWeb Current_Web = site.OpenWeb())
                        {
                            properties.Web.AllowUnsafeUpdates = true;
                            Current_Web.AllowUnsafeUpdates = true;
                            SPList Current = Current_Web.Lists[properties.ListId];
                            SPListItem Current_Item = Current.GetItemById(properties.ListItemId);
                            SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection();
                            fieldUserValues.Add(new SPFieldUserValue(Current_Web, properties.Web.CurrentUser.ID, properties.Web.CurrentUser.Name));
                            Current_Item[Constants.Viewd_By_field] = fieldUserValues;
                            Current_Item.SystemUpdate(false);
                            properties.Web.AllowUnsafeUpdates = false;
                            Current_Web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
            catch
            {
            }
            base.ItemUpdated(properties);
           
        }
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            try
            {
                using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                {
                    SPUserToken systoken = properties.ListItem.Web.Site.SystemAccount.UserToken;
                    using (SPSite site = new SPSite(properties.ListItem.Web.Site.Url, systoken))
                    {
                        using (SPWeb Current_Web = site.OpenWeb())
                        {
                            properties.Web.AllowUnsafeUpdates = true;
                            Current_Web.AllowUnsafeUpdates = true;
                            SPList Current = Current_Web.Lists[properties.ListId];
                            SPListItem Current_Item = Current.GetItemById(properties.ListItemId);
                            SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection();
                            fieldUserValues.Add(new SPFieldUserValue(Current_Web, properties.Web.CurrentUser.ID, properties.Web.CurrentUser.Name));
                            Current_Item[Constants.Viewd_By_field] = fieldUserValues;
                            Current_Item.SystemUpdate(false);
                            properties.Web.AllowUnsafeUpdates = false;
                            Current_Web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
            catch
            {
            }
        }


    }
}
